/*
 * 
 * 
 * 
 */
package com.hboxs.asl.plugin.file;

import com.hboxs.asl.FileInfo;
import com.hboxs.asl.Setting;
import com.hboxs.asl.plugin.StoragePlugin;
import com.hboxs.asl.util.SettingUtils;
import org.apache.commons.io.FileUtils;
import org.springframework.stereotype.Component;
import org.springframework.web.context.ServletContextAware;

import javax.servlet.ServletContext;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Plugin - 本地文件存储
 */
@Component("filePlugin")
public class FilePlugin extends StoragePlugin implements ServletContextAware {

    /**
     * 静态文件根目录名
     */
    private static final String ROOT_FOLDER_NAME = "i";
    private static final String PATH = "/asl-admin/storage_plugin/file/list.htm";

    /**
     * servletContext
     */
    private ServletContext servletContext;

    public void setServletContext(ServletContext servletContext) {
        this.servletContext = servletContext;
    }

    @Override
    public String getName() {
        return "本地文件存储";
    }

    @Override
    public String getVersion() {
        return "1.0";
    }

    @Override
    public String getAuthor() {
        return SettingUtils.get().getSiteName();
    }

    @Override
    public String getSiteUrl() {
        return SettingUtils.get().getSiteUrl() + PATH;
    }

    @Override
    public String getInstallUrl() {
        return null;
    }

    @Override
    public String getUninstallUrl() {
        return null;
    }

    @Override
    public String getSettingUrl() {
        return "file/setting.htm";
    }

    @Override
    public void upload(String path, File file, String contentType) {
        Setting setting = SettingUtils.get();
        File destFile = new File(setting.getRootPath() + path);
//		File destFile = new File(setting.getStaticRoot() + path);
        try {
            FileUtils.moveFile(file, destFile);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public String getUrl(String path) {
        Setting setting = SettingUtils.get();
        return setting.getSiteUrl() + "/" + ROOT_FOLDER_NAME + path;
    }

    @Override
    public List<FileInfo> browser(String path) {
        Setting setting = SettingUtils.get();
        List<FileInfo> fileInfos = new ArrayList<FileInfo>();
//		File directory = new File(servletContext.getRealPath(path));
        File directory = new File(setting.getRootPath() + path);
        if (directory.exists() && directory.isDirectory()) {
            for (File file : directory.listFiles()) {
                FileInfo fileInfo = new FileInfo();
                fileInfo.setName(file.getName());
                fileInfo.setUrl(setting.getSiteUrl() + "/" + ROOT_FOLDER_NAME + path + file.getName());
                fileInfo.setIsDirectory(file.isDirectory());
                fileInfo.setSize(file.length());
                fileInfo.setLastModified(new Date(file.lastModified()));
                fileInfos.add(fileInfo);
            }
        }
        return fileInfos;
    }

}